package com.js.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.js.pojo.Student;
import com.js.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {
    private StudentService studentService;
    @Autowired
    @Qualifier("StudentServiceImpl")
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
    @RequestMapping("/queryAll")
    public String queryAllStudent(Model model){
        List<Student> studentList = studentService.queryAllStudent();
        model.addAttribute("studentList",studentList);
        System.out.println(studentList);
        return "student/studentLogin";
    }
    @RequestMapping("/queryAll1")
    public String queryAllStudent1(Model model){
        List<Student> studentList = studentService.queryAllStudent();
        model.addAttribute("studentList",studentList);
        System.out.println(studentList);
        return "student/studentList";
    }
    //跳转到添加学生的页面
    @RequestMapping("/toAdd")
    public String toAddStudent(){
        return "student/addStudent";
    }
    @RequestMapping("/toAdd1")
    public String toAddStudent1(){
        return "student/registerStudent";
    }
    //添加学生的功能请求
    @RequestMapping("/add")
    public String addStudent(Student student){
        System.out.println("addStudent==>"+student.toString());
        int i = studentService.addStudent(student);
        if (i>0){
            return "redirect:/student/queryStudentByPage";//添加学生后需要，重定向到查询全部学生
        }
        return "redirect:/student/toAdd";
    }
    @RequestMapping("/add1")
    public String addStudent1(Student student){
        System.out.println("addStudent==>"+student.toString());
        int i = studentService.addStudent(student);
        if (i>0){
            return "student/studentLogin";//添加学生后需要，重定向到查询全部学生
        }else {
            return "redirect:/student/toAdd1";
        }
    }
    @RequestMapping("/dele/{id}")
   public String deleteStudentById(@PathVariable("id") int id){
        int i = studentService.deleteStudentById(id);
        System.out.println(i);
        if (i>0){
            System.out.println("删除成功！！");
        }
        return "redirect:/student/queryStudentByPage";
    }
    @RequestMapping("/toUpdate")
    public String toUpdateStudent(int id, Model model){
        Student student = studentService.queryStudentById(id);
        model.addAttribute("QStudent",student);
        return "student/updateStudent";
    }

    @RequestMapping("/toUpdate1")
    public String toUpdateStudent1(){
        return "student/updateStudent1";
    }
    @RequestMapping("/update")
    public String updateStudent(Student student, Model model){
        System.out.println("updateStudent==>"+student.toString());
        int i = studentService.updateStudent(student);
        if (i>0){
            return "redirect:/student/queryStudentByPage";//添加学生后需要，重定向到查询全部学生
        }
        return "redirect:/student/toUpdate";
    }
    @RequestMapping("/update1")
    public String updateStudent1(Student student, Model model){
        System.out.println("updateStudent==>"+student.toString());
        int i = studentService.updateStudent(student);
        if (i>0){
            return "updateSuccess";
        }
        return "redirect:/student/toUpdate1";
    }
    @RequestMapping("/queryId")
    @ResponseBody
    public String queryStudentById(){
        Student student = studentService.queryStudentById(1);
        System.out.println(student);
        return student.toString();
    }
    @ResponseBody
    @RequestMapping("/queryAccount")
    public String queryStudentByAccount(){
        Student student = studentService.queryStudentByAccount("2116190108");
        System.out.println(student);
        return student.toString();
    }
    @RequestMapping("/queryName")
    public String queryStudentByName(String studentName, Model model){
        System.out.println("要查询的学生:" + studentName);
        List<Student> students = studentService.queryStudentByName(studentName);
        PageHelper.startPage(1,4);
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        model.addAttribute("page",pageInfo);
        return "student/studentList";
    }

    /***
     * 分页
     * @param model
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryStudentByPage")
    public String queryBookByPage(Model model,
                                  @RequestParam(required = true,defaultValue = "1") Integer page,
                                  @RequestParam(required = false,defaultValue = "4")Integer pageSize){
        //page默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录。
        PageHelper.startPage(page,pageSize);
        List<Student> studentsList = studentService.queryAllStudent();
        PageInfo<Student> pageInfo = new PageInfo<>(studentsList);
        model.addAttribute("page",pageInfo);
        return "student/studentList";
    }
}
